package com.ly.system.system.bean.vo;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.ly.common.core.utils.UrlUtils;
import com.ly.system.system.bean.entity.SysPermission;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单-vo
 *
 * @author luoyun
 */
@Data
@NoArgsConstructor
@Schema(description = "菜单-vo")
public class MenuVO implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    private String id;
    /**
     * 父id
     */
    private String parentId;
    /**
     * 路由
     */
    private String route;

    /**
     * 路径
     */
    private String path;

    /**
     * 名称
     */
    private String name;

    /**
     * 是否隐藏
     */
    private Boolean hidden;
    private Boolean alwaysShow;
    /**
     * 外链
     */
    private Boolean internalOrExternal;
    /**
     * 组件名称
     */
    private String component;
    /**
     * 路径
     */
    private String redirect;

    /**
     * 元数据
     */
    private Meta meta;

    /**
     * 子节点
     */
    private List<MenuVO> children = new ArrayList<>();

    // {
    //     componentName: "index",
    //     hideTab: false,
    //     icon: "ant-design:home-outlined",
    //     title: "主页"
    // }
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    static class Meta {
        /**
         * 标题
         */
        private String title;
        /**
         * 图标
         */
        private String icon;
        /**
         * 是否隐藏
         */
        private Boolean hideTab;
        /**
         * 是否保活
         */
        private Boolean keepAlive;
        private String url;
        private String componentName;
        // vue3版本兼容代码
        private Boolean hideMenu;
    }

    public MenuVO(SysPermission n) {
        this.id = n.getId();
        this.parentId = n.getParentId();
        this.route = ObjUtil.equal(n.getRoute(), true) ? "1" : "0";
        this.path = UrlUtils.isWwwHttpUrl(n.getUrl()) ? DigestUtil.md5Hex(n.getUrl()) : n.getUrl();
        this.name = StrUtil.isNotEmpty(n.getComponentName()) ? n.getComponentName() : n.getUrl();
        this.hidden = n.getHidden();
        this.alwaysShow = ObjUtil.equals(n.getAlwaysShow(), true);
        this.internalOrExternal = ObjUtil.equals(n.getInternalOrExternal(), true);
        this.component = n.getComponent();
        this.redirect = n.getRedirect();
        this.meta = Meta.builder()
                .hideMenu(n.getHidden())
                .title(n.getName())
                .componentName(n.getComponentName())
                .url(n.getUrl())
                .hideTab(n.getHideTab())
                .keepAlive(ObjUtil.equals(n.getKeepAlive(), true))
                .icon(n.getIcon())
                .build();
    }
}
